package nuaa.dao;

import nuaa.domain.Group;
import nuaa.domain.OrdinaryUser;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 数据库的持久层操作
 */
public interface IGroupDao {

    /**
     * 根据groupId查询相应记录
     * @param groupId
     * @return
     */
    @Select("select * from `group` where groupId=#{groupId}")
    Group findByGroupId(Integer groupId);

    /**
     * 根据groupId查询组内所有的成员
     * @param groupId
     * @return
     */
    @Select("select * from ordinaryuser where groupId=#{groupId}")
    List<OrdinaryUser> findAllUsers(Integer groupId);

    /**
     * 向group数据表中插入新的group
     * @param group
     */
    @Insert("insert into `group` (groupName,leaderId,numOfPeople,destinations," +
            "meetingPlace,gatheringTime,duration) values(#{groupName},#{leaderId}," +
            "#{numOfPeople},#{destinations},#{meetingPlace},#{gatheringTime}," +
            "#{duration})")
    @Options(useGeneratedKeys = true, keyProperty = "groupId")
    void addNewGroup(Group group);

    /**
     * 根据队伍Id删除队伍
     * @param groupId
     */
    @Delete("delete from `group` where groupId=#{groupId}")
    void deleteGroupByGroupId(Integer groupId);

    @Select("select destinations from `group` where groupId=#{groupId}")
    String findDestinationsByGroupId(Integer groupId);

}
